package com.devcon5.StudentPlanner;

import baseclasses.Assignment;
import baseclasses.Reminder;
import database.AssignmentDAO;
import database.ReminderDAO;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import android.os.Bundle;

public class AlarmReceiver extends BroadcastReceiver {
	
	public static String ALARM_ID = "alarmid";

 @Override
 public void onReceive(Context context, Intent intent) {
	 
   try {
	   
     Bundle bundle = intent.getExtras();
     int assignmentID = bundle.getInt(ALARM_ID);	//since the assignment ID is bundled in the reminder class, unpack it
     Assignment assignment = AssignmentDAO.getAssignmentById(context, assignmentID);	//retrieve the assignment by ID
     Reminder reminder = ReminderDAO.getReminderByAssignment(context, assignment);	//get the reminder that was just created in this process
     ReminderDAO.deleteReminder(context, reminder); //delete the reminder since it will be used to trigger this notification
     
     
     NotificationManager nManager = (NotificationManager)context.getSystemService(Service.NOTIFICATION_SERVICE); //get a notification manager
     Notification notification = new Notification(R.drawable.check, assignment.getName()+" due soon.", System.currentTimeMillis());	
     //Displays notification that shows "_____" due soon at the current time (time is matched with the time in the reminder object)
     
   
     Intent newIntent = new Intent(context, PlannerTabManager.class);	//if they click this notification, they should be sent to the DevCon5 application
     																	//so prepare this intent for use in a pending intent
     newIntent.addFlags( Intent.FLAG_ACTIVITY_SINGLE_TOP );	//flags newIntent so if it's already on top, it won't be launched
     PendingIntent pendingintent = PendingIntent.getActivity(context, 4, newIntent, PendingIntent.FLAG_CANCEL_CURRENT);
     //get the application as a new activity given request code 4; if it's already been fired, don't launch a new one
     //this takes care of overlapping alarms
     
     //Displays "____ due <due date due time>" in the notification area once the user pulls the status bar down
     notification.setLatestEventInfo(context, assignment.getName(),"due "+assignment.getDueDateString("MM/dd hh:mm"), pendingintent);
     
     //***********Want to replace with notification builder to eliminate | (or) since it's bitwise*******
     
     notification.defaults |= Notification.DEFAULT_LIGHTS;	//use the default notification lights
     notification.defaults |= Notification.DEFAULT_VIBRATE;	//just vibrate the phone
     notification.ledARGB = 0xff00ff00;	//flash the green light on the phone
     notification.ledOnMS = 300;	//for 300ms
     notification.ledOffMS = 1000;	//pause for 1000ms
     notification.flags |= Notification.FLAG_SHOW_LIGHTS; //Show the lights
     notification.flags |= Notification.FLAG_AUTO_CANCEL; //cancel the lights & vibration if the user clicks the status bar
     
     nManager.notify(4, notification);	//deliver the above notification
     
    } catch (Exception e) {
     Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
     e.printStackTrace();

    }
 }

}